// 定义路由
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter)

// 导入对应的页面组件
// import Home from "@/view/Home";
// import Topic from "@/view/Topic";
// import Cartgory from "@/view/Cartgory";
// import Car from "@/view/Car";
// import User from "@/view/User";
// import Search from "@/view/Search";
// import ProductDetail from "@/view/ProductDetail";

// 配置路由规则
const routes = [
    {
        path: '/',
        redirect: '/home',
    },
    {
        path: '/home',
        component: () => import("@/view/Home"),
        name: 'home',
        meta: {
            isshowtanbar: true //meta 字段为路由元信息
        },
        children: [
            {
                path: 'search',
                component: () => import("@/view/Search"), //搜索页定义二级路由
                name: 'search',
                meta: {
                    isshowtanbar: false
                },
            }
        ]
    },
    {
        path: '/topic',
        component: () => import("@/view/Topic"),
        name: 'topic',
        meta: {
            isshowtanbar: true
        },
    },
    {
        path: '/cargory',
        component: () => import("@/view/Cartgory"),
        name: 'cargory',
        meta: {
            isshowtanbar: true
        },
    },
    {
        path: '/car',
        component: () => import("@/view/Car"),
        name: 'car',
        meta: {
            isshowtanbar: true
        },
    },
    {
        path: '/user',
        component: () => import("@/view/User"),
        name: 'user',
        meta: {
            isshowtanbar: true
        },
    },
    {
        path: '/productDetail',
        component: () => import("@/view/ProductDetail"),
        name: 'productDetail',
        meta: {
            isshowtanbar: false
        },
    },
    {
        path: '/categoryList',
        component: () => import("@/view/Categorylist"),
        name: 'categoryList',
        meta: {
            isshowtanbar: false
        },
        props:true,
    },
    {
        path: '/historyhot',
        component: () => import("@/components/HistoryHot"),
        name: 'historyhot',
        meta: {
            isshowtanbar: false
        },
        props:true,
    }


]

// 创建路由实例
const router = new VueRouter({
    mode: 'history',
    routes: routes
})

// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
    return originalPush.call(this, location).catch(err => { })
};

// 创建路由前置守卫
// 监控全局所有的路由导航
router.beforeEach((to, from, next) => {
    next()
    // 判断当访问购物车页的时候,是否用户已经登录,如果未登录,跳转到登录页面
    // 如果已经登陆,就放行
    if (to.path == '/car') {
        // 表示访问的是购物车页
        const token = localStorage.getItem("token") //返回值,要么是字符串 或者null
        if (token) {
            next()
        } else {
            // 跳转到登录页面
            // 提示请先登录
            Vue.prototype.$toast({
                type: 'fail',
                message: '请先登录',
                duration: 1000
            });
            setTimeout(() => {
                next('/user')
            }, 1000)

        }
    } else {
        // 非购物车页可以直接放行
        next()
    }
})

// 导出路由实例对象
export default router